package de.hwrberlin.remo.diagram.part;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.gmf.tooling.runtime.update.DiagramUpdater;

import de.hwrberlin.remo.diagram.edit.parts.AndRuleEditPart;
import de.hwrberlin.remo.diagram.edit.parts.BooleanFactEditPart;
import de.hwrberlin.remo.diagram.edit.parts.ImplicationEditPart;
import de.hwrberlin.remo.diagram.edit.parts.KnowledgeBaseEditPart;
import de.hwrberlin.remo.diagram.edit.parts.LinksFromFactsEditPart;
import de.hwrberlin.remo.diagram.edit.parts.LinksFromRulesEditPart;
import de.hwrberlin.remo.diagram.edit.parts.OrRuleEditPart;
import de.hwrberlin.remo.diagram.providers.RemoElementTypes;
import de.hwrberlin.remo.model.AndRule;
import de.hwrberlin.remo.model.BooleanFact;
import de.hwrberlin.remo.model.Fact;
import de.hwrberlin.remo.model.Implication;
import de.hwrberlin.remo.model.KnowledgeBase;
import de.hwrberlin.remo.model.KnowledgeBaseElement;
import de.hwrberlin.remo.model.LinksFromFacts;
import de.hwrberlin.remo.model.LinksFromRules;
import de.hwrberlin.remo.model.OrRule;
import de.hwrberlin.remo.model.RemoModelPackage;
import de.hwrberlin.remo.model.Rule;

/**
 * @generated
 */
public class RemoDiagramUpdater {

	/**
	 * @generated
	 */
	public static List<RemoNodeDescriptor> getSemanticChildren(View view) {
		switch (RemoVisualIDRegistry.getVisualID(view)) {
		case KnowledgeBaseEditPart.VISUAL_ID:
			return getKnowledgeBase_1000SemanticChildren(view);
		}
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<RemoNodeDescriptor> getKnowledgeBase_1000SemanticChildren(
			View view) {
		if (!view.isSetElement()) {
			return Collections.emptyList();
		}
		KnowledgeBase modelElement = (KnowledgeBase) view.getElement();
		LinkedList<RemoNodeDescriptor> result = new LinkedList<RemoNodeDescriptor>();
		for (Iterator<?> it = modelElement.getOwnedElements().iterator(); it
				.hasNext();) {
			KnowledgeBaseElement childElement = (KnowledgeBaseElement) it
					.next();
			int visualID = RemoVisualIDRegistry.getNodeVisualID(view,
					childElement);
			if (visualID == BooleanFactEditPart.VISUAL_ID) {
				result.add(new RemoNodeDescriptor(childElement, visualID));
				continue;
			}
			if (visualID == AndRuleEditPart.VISUAL_ID) {
				result.add(new RemoNodeDescriptor(childElement, visualID));
				continue;
			}
			if (visualID == ImplicationEditPart.VISUAL_ID) {
				result.add(new RemoNodeDescriptor(childElement, visualID));
				continue;
			}
			if (visualID == OrRuleEditPart.VISUAL_ID) {
				result.add(new RemoNodeDescriptor(childElement, visualID));
				continue;
			}
		}
		return result;
	}

	/**
	 * @generated
	 */
	public static List<RemoLinkDescriptor> getContainedLinks(View view) {
		switch (RemoVisualIDRegistry.getVisualID(view)) {
		case KnowledgeBaseEditPart.VISUAL_ID:
			return getKnowledgeBase_1000ContainedLinks(view);
		case BooleanFactEditPart.VISUAL_ID:
			return getBooleanFact_2005ContainedLinks(view);
		case AndRuleEditPart.VISUAL_ID:
			return getAndRule_2004ContainedLinks(view);
		case ImplicationEditPart.VISUAL_ID:
			return getImplication_2007ContainedLinks(view);
		case OrRuleEditPart.VISUAL_ID:
			return getOrRule_2006ContainedLinks(view);
		case LinksFromRulesEditPart.VISUAL_ID:
			return getLinksFromRules_4005ContainedLinks(view);
		case LinksFromFactsEditPart.VISUAL_ID:
			return getLinksFromFacts_4006ContainedLinks(view);
		}
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<RemoLinkDescriptor> getIncomingLinks(View view) {
		switch (RemoVisualIDRegistry.getVisualID(view)) {
		case BooleanFactEditPart.VISUAL_ID:
			return getBooleanFact_2005IncomingLinks(view);
		case AndRuleEditPart.VISUAL_ID:
			return getAndRule_2004IncomingLinks(view);
		case ImplicationEditPart.VISUAL_ID:
			return getImplication_2007IncomingLinks(view);
		case OrRuleEditPart.VISUAL_ID:
			return getOrRule_2006IncomingLinks(view);
		case LinksFromRulesEditPart.VISUAL_ID:
			return getLinksFromRules_4005IncomingLinks(view);
		case LinksFromFactsEditPart.VISUAL_ID:
			return getLinksFromFacts_4006IncomingLinks(view);
		}
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<RemoLinkDescriptor> getOutgoingLinks(View view) {
		switch (RemoVisualIDRegistry.getVisualID(view)) {
		case BooleanFactEditPart.VISUAL_ID:
			return getBooleanFact_2005OutgoingLinks(view);
		case AndRuleEditPart.VISUAL_ID:
			return getAndRule_2004OutgoingLinks(view);
		case ImplicationEditPart.VISUAL_ID:
			return getImplication_2007OutgoingLinks(view);
		case OrRuleEditPart.VISUAL_ID:
			return getOrRule_2006OutgoingLinks(view);
		case LinksFromRulesEditPart.VISUAL_ID:
			return getLinksFromRules_4005OutgoingLinks(view);
		case LinksFromFactsEditPart.VISUAL_ID:
			return getLinksFromFacts_4006OutgoingLinks(view);
		}
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<RemoLinkDescriptor> getKnowledgeBase_1000ContainedLinks(
			View view) {
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<RemoLinkDescriptor> getAndRule_2004ContainedLinks(
			View view) {
		AndRule modelElement = (AndRule) view.getElement();
		LinkedList<RemoLinkDescriptor> result = new LinkedList<RemoLinkDescriptor>();
		result.addAll(getContainedTypeModelFacetLinks_LinksFromRules_4005(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<RemoLinkDescriptor> getImplication_2007ContainedLinks(
			View view) {
		Implication modelElement = (Implication) view.getElement();
		LinkedList<RemoLinkDescriptor> result = new LinkedList<RemoLinkDescriptor>();
		result.addAll(getContainedTypeModelFacetLinks_LinksFromRules_4005(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<RemoLinkDescriptor> getBooleanFact_2005ContainedLinks(
			View view) {
		BooleanFact modelElement = (BooleanFact) view.getElement();
		LinkedList<RemoLinkDescriptor> result = new LinkedList<RemoLinkDescriptor>();
		result.addAll(getContainedTypeModelFacetLinks_LinksFromFacts_4006(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<RemoLinkDescriptor> getOrRule_2006ContainedLinks(
			View view) {
		OrRule modelElement = (OrRule) view.getElement();
		LinkedList<RemoLinkDescriptor> result = new LinkedList<RemoLinkDescriptor>();
		result.addAll(getContainedTypeModelFacetLinks_LinksFromRules_4005(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<RemoLinkDescriptor> getLinksFromRules_4005ContainedLinks(
			View view) {
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<RemoLinkDescriptor> getLinksFromFacts_4006ContainedLinks(
			View view) {
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<RemoLinkDescriptor> getAndRule_2004IncomingLinks(
			View view) {
		AndRule modelElement = (AndRule) view.getElement();
		Map<EObject, Collection<EStructuralFeature.Setting>> crossReferences = EcoreUtil.CrossReferencer
				.find(view.eResource().getResourceSet().getResources());
		LinkedList<RemoLinkDescriptor> result = new LinkedList<RemoLinkDescriptor>();
		result.addAll(getIncomingTypeModelFacetLinks_LinksFromRules_4005(
				modelElement, crossReferences));
		result.addAll(getIncomingTypeModelFacetLinks_LinksFromFacts_4006(
				modelElement, crossReferences));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<RemoLinkDescriptor> getImplication_2007IncomingLinks(
			View view) {
		Implication modelElement = (Implication) view.getElement();
		Map<EObject, Collection<EStructuralFeature.Setting>> crossReferences = EcoreUtil.CrossReferencer
				.find(view.eResource().getResourceSet().getResources());
		LinkedList<RemoLinkDescriptor> result = new LinkedList<RemoLinkDescriptor>();
		result.addAll(getIncomingTypeModelFacetLinks_LinksFromRules_4005(
				modelElement, crossReferences));
		result.addAll(getIncomingTypeModelFacetLinks_LinksFromFacts_4006(
				modelElement, crossReferences));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<RemoLinkDescriptor> getBooleanFact_2005IncomingLinks(
			View view) {
		BooleanFact modelElement = (BooleanFact) view.getElement();
		Map<EObject, Collection<EStructuralFeature.Setting>> crossReferences = EcoreUtil.CrossReferencer
				.find(view.eResource().getResourceSet().getResources());
		LinkedList<RemoLinkDescriptor> result = new LinkedList<RemoLinkDescriptor>();
		result.addAll(getIncomingTypeModelFacetLinks_LinksFromRules_4005(
				modelElement, crossReferences));
		result.addAll(getIncomingTypeModelFacetLinks_LinksFromFacts_4006(
				modelElement, crossReferences));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<RemoLinkDescriptor> getOrRule_2006IncomingLinks(View view) {
		OrRule modelElement = (OrRule) view.getElement();
		Map<EObject, Collection<EStructuralFeature.Setting>> crossReferences = EcoreUtil.CrossReferencer
				.find(view.eResource().getResourceSet().getResources());
		LinkedList<RemoLinkDescriptor> result = new LinkedList<RemoLinkDescriptor>();
		result.addAll(getIncomingTypeModelFacetLinks_LinksFromRules_4005(
				modelElement, crossReferences));
		result.addAll(getIncomingTypeModelFacetLinks_LinksFromFacts_4006(
				modelElement, crossReferences));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<RemoLinkDescriptor> getLinksFromRules_4005IncomingLinks(
			View view) {
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<RemoLinkDescriptor> getLinksFromFacts_4006IncomingLinks(
			View view) {
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<RemoLinkDescriptor> getAndRule_2004OutgoingLinks(
			View view) {
		AndRule modelElement = (AndRule) view.getElement();
		LinkedList<RemoLinkDescriptor> result = new LinkedList<RemoLinkDescriptor>();
		result.addAll(getContainedTypeModelFacetLinks_LinksFromRules_4005(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<RemoLinkDescriptor> getImplication_2007OutgoingLinks(
			View view) {
		Implication modelElement = (Implication) view.getElement();
		LinkedList<RemoLinkDescriptor> result = new LinkedList<RemoLinkDescriptor>();
		result.addAll(getContainedTypeModelFacetLinks_LinksFromRules_4005(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<RemoLinkDescriptor> getBooleanFact_2005OutgoingLinks(
			View view) {
		BooleanFact modelElement = (BooleanFact) view.getElement();
		LinkedList<RemoLinkDescriptor> result = new LinkedList<RemoLinkDescriptor>();
		result.addAll(getContainedTypeModelFacetLinks_LinksFromFacts_4006(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<RemoLinkDescriptor> getOrRule_2006OutgoingLinks(View view) {
		OrRule modelElement = (OrRule) view.getElement();
		LinkedList<RemoLinkDescriptor> result = new LinkedList<RemoLinkDescriptor>();
		result.addAll(getContainedTypeModelFacetLinks_LinksFromRules_4005(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List<RemoLinkDescriptor> getLinksFromRules_4005OutgoingLinks(
			View view) {
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	public static List<RemoLinkDescriptor> getLinksFromFacts_4006OutgoingLinks(
			View view) {
		return Collections.emptyList();
	}

	/**
	 * @generated
	 */
	private static Collection<RemoLinkDescriptor> getContainedTypeModelFacetLinks_LinksFromRules_4005(
			Rule container) {
		LinkedList<RemoLinkDescriptor> result = new LinkedList<RemoLinkDescriptor>();
		for (Iterator<?> links = container.getLinksrules().iterator(); links
				.hasNext();) {
			EObject linkObject = (EObject) links.next();
			if (false == linkObject instanceof LinksFromRules) {
				continue;
			}
			LinksFromRules link = (LinksFromRules) linkObject;
			if (LinksFromRulesEditPart.VISUAL_ID != RemoVisualIDRegistry
					.getLinkWithClassVisualID(link)) {
				continue;
			}
			KnowledgeBaseElement dst = link.getElement();
			result.add(new RemoLinkDescriptor(container, dst, link,
					RemoElementTypes.LinksFromRules_4005,
					LinksFromRulesEditPart.VISUAL_ID));
		}
		return result;
	}

	/**
	 * @generated
	 */
	private static Collection<RemoLinkDescriptor> getContainedTypeModelFacetLinks_LinksFromFacts_4006(
			Fact container) {
		LinkedList<RemoLinkDescriptor> result = new LinkedList<RemoLinkDescriptor>();
		for (Iterator<?> links = container.getLinksfacts().iterator(); links
				.hasNext();) {
			EObject linkObject = (EObject) links.next();
			if (false == linkObject instanceof LinksFromFacts) {
				continue;
			}
			LinksFromFacts link = (LinksFromFacts) linkObject;
			if (LinksFromFactsEditPart.VISUAL_ID != RemoVisualIDRegistry
					.getLinkWithClassVisualID(link)) {
				continue;
			}
			KnowledgeBaseElement dst = link.getElement();
			result.add(new RemoLinkDescriptor(container, dst, link,
					RemoElementTypes.LinksFromFacts_4006,
					LinksFromFactsEditPart.VISUAL_ID));
		}
		return result;
	}

	/**
	 * @generated
	 */
	private static Collection<RemoLinkDescriptor> getIncomingTypeModelFacetLinks_LinksFromRules_4005(
			KnowledgeBaseElement target,
			Map<EObject, Collection<EStructuralFeature.Setting>> crossReferences) {
		LinkedList<RemoLinkDescriptor> result = new LinkedList<RemoLinkDescriptor>();
		Collection<EStructuralFeature.Setting> settings = crossReferences
				.get(target);
		for (EStructuralFeature.Setting setting : settings) {
			if (setting.getEStructuralFeature() != RemoModelPackage.eINSTANCE
					.getLinksFromRules_Element()
					|| false == setting.getEObject() instanceof LinksFromRules) {
				continue;
			}
			LinksFromRules link = (LinksFromRules) setting.getEObject();
			if (LinksFromRulesEditPart.VISUAL_ID != RemoVisualIDRegistry
					.getLinkWithClassVisualID(link)) {
				continue;
			}
			if (false == link.eContainer() instanceof Rule) {
				continue;
			}
			Rule container = (Rule) link.eContainer();
			result.add(new RemoLinkDescriptor(container, target, link,
					RemoElementTypes.LinksFromRules_4005,
					LinksFromRulesEditPart.VISUAL_ID));

		}
		return result;
	}

	/**
	 * @generated
	 */
	private static Collection<RemoLinkDescriptor> getIncomingTypeModelFacetLinks_LinksFromFacts_4006(
			KnowledgeBaseElement target,
			Map<EObject, Collection<EStructuralFeature.Setting>> crossReferences) {
		LinkedList<RemoLinkDescriptor> result = new LinkedList<RemoLinkDescriptor>();
		Collection<EStructuralFeature.Setting> settings = crossReferences
				.get(target);
		for (EStructuralFeature.Setting setting : settings) {
			if (setting.getEStructuralFeature() != RemoModelPackage.eINSTANCE
					.getLinksFromFacts_Element()
					|| false == setting.getEObject() instanceof LinksFromFacts) {
				continue;
			}
			LinksFromFacts link = (LinksFromFacts) setting.getEObject();
			if (LinksFromFactsEditPart.VISUAL_ID != RemoVisualIDRegistry
					.getLinkWithClassVisualID(link)) {
				continue;
			}
			if (false == link.eContainer() instanceof Fact) {
				continue;
			}
			Fact container = (Fact) link.eContainer();
			result.add(new RemoLinkDescriptor(container, target, link,
					RemoElementTypes.LinksFromFacts_4006,
					LinksFromFactsEditPart.VISUAL_ID));

		}
		return result;
	}

	/**
	 * @generated
	 */
	public static final DiagramUpdater TYPED_INSTANCE = new DiagramUpdater() {
		/**
		 * @generated
		 */
		@Override
		public List<RemoNodeDescriptor> getSemanticChildren(View view) {
			return RemoDiagramUpdater.getSemanticChildren(view);
		}

		/**
		 * @generated
		 */
		@Override
		public List<RemoLinkDescriptor> getContainedLinks(View view) {
			return RemoDiagramUpdater.getContainedLinks(view);
		}

		/**
		 * @generated
		 */
		@Override
		public List<RemoLinkDescriptor> getIncomingLinks(View view) {
			return RemoDiagramUpdater.getIncomingLinks(view);
		}

		/**
		 * @generated
		 */
		@Override
		public List<RemoLinkDescriptor> getOutgoingLinks(View view) {
			return RemoDiagramUpdater.getOutgoingLinks(view);
		}
	};

}
